<?php
	/**
	 * Standard pagination renderer
	 * 
	 * @package Pagination
	 * @author Perkin Anton, Rostislav Rotaru
	 * @version 3.0
	 *
	 */
	class StandardPagination extends PaginationBase 
	{
		
		/**
		 * Renders the pagination according to the schema
		 * 
		 * @param array $schema scema to be renderred
		 */
		public function Render($schema)
		{
			$i = 1;			
			$items = '';
			$dotsAdded = false;
			$result = '';
			
			if (isset($schema['items']))
			{
				foreach ($schema['items'] as $name => $item)
				{
					if ($name == $schema['selected_page'])
					{
						//Get the tab item renderer
						$paginationItemRenderer = PaginationConfig::CheckPool($item['selected_render_class'], 'paginationitem');
						
						//tab's content
						$items .= $paginationItemRenderer->Render($name, $item);
					}
					else
					{
						if ($this->CheckVisibility($i, $schema))
						{
							//Get the tab item renderer
							$paginationItemRenderer = PaginationConfig::CheckPool($item['render_class'], 'paginationitem');
							
							//tab's content
							$items .= $paginationItemRenderer->Render($name, $item);
							
							$dotsAdded = false;
						}
						else
						{
							if (!$dotsAdded)
							{
								$items .= "<div class = '".$schema['css_gap_class']."'>...</div>";
								$dotsAdded = true;
							}
						}
					}
					
					$i++;
				}
			}
			$result .= '<div class = "'.$schema['css_class'].'">';
				
			if ($schema['selected_page'] == $schema['start_page'])
			{
				$result .=  '<div class = "pagination_button '.$schema['theme'].'_first_disabled_button">&nbsp;</div>';
				$result .=  '<div class = "pagination_button '.$schema['theme'].'_prev_disabled_button">&nbsp;</div>';
			}
			else
			{
				$result .=  '<a '.str_replace('{page}', $schema['start_page'], $schema['page_link_attributes']).' class = "'.$schema['theme'].'_clean"><div class = "pagination_button '.$schema['theme'].'_first_active_button">&nbsp;</div></a>';
				$result .=  '<a '.str_replace('{page}', ($schema['selected_page'] - $schema['step']), $schema['page_link_attributes']).' class = "'.$schema['theme'].'_clean"><div class = "pagination_button '.$schema['theme'].'_prev_active_button">&nbsp;</div></a>';
			}
			$result .=  $items;
			
			if ($schema['selected_page'] == $schema['pages_number'])
			{
				$result .=  '<div class = "pagination_button '.$schema['theme'].'_next_disabled_button">&nbsp;</div>';
				$result .=  '<div class = "pagination_button '.$schema['theme'].'_last_disabled_button">&nbsp;</div>';
			}
			else
			{
				$result .=  '<a '.str_replace('{page}', ($schema['selected_page'] + $schema['step']), $schema['page_link_attributes']).' class = "'.$schema['theme'].'_clean"><div class = "pagination_button '.$schema['theme'].'_next_active_button">&nbsp;</div></a>';
				$result .=  '<a '.str_replace('{page}', $schema['pages_number'], $schema['page_link_attributes']).' class = "'.$schema['theme'].'_clean"><div class = "pagination_button '.$schema['theme'].'_last_active_button">&nbsp;</div></a>';
			}
			
			if ($schema['use_custom_page_number'])
			{
				$result .=  '<form method = "GET" action = "" style = "display: inline;">';
				$result .=  '<div class = "'.$schema['theme'].'_page_number_label">'.$schema['custom_page_number_title'].'</div><input name = "'.$schema['custom_page_number_name'].'" value = "'.$schema['selected_page'].'" class = "'.$schema['css_input_class'].'" />';
				$result .=  '</form>';
			}
			
			$result .=  '</div>';
			
			return $result;
		}
		
		/**
		 * Cheacks whether the item should be displayed.
		 *
		 * @param integer $pageNumber page number.
		 * @param array $schema schema
		 * @return boolean
		 */
		public function CheckVisibility($pageNumber, $schema)
		{
			if (in_array($pageNumber, $schema['visible_items'])) 
			{
				return true;
			}

			return false;
		}
	}
?>